 

--- 游戏控制表
local game = {}
ac.game = game

--- 派发全局事件(有返回值)
--- @param name string
--- @param ... any
--- @return function
function game:dispatch(name, ...)
	return ac.dispatch(self, name, ...)
end

--- 派发全局事件
--- @param name string
--- @param ... any
--- @return function
function game:notify(name, ...)
	return ac.notify(self, name, ...)
end

--- 注册全局事件
--- @param name string
--- @return function
function game:on(name)
	return ac.register(self, name)
end

--- 注册全局事件
--- @param name string
--- @return function
function game:event(name)
	return ac.register(self, name)
end

--- 注册全局事件
--- @param name string
--- @return function
function game:register(name)
	return ac.register(self, name)
end

--- 打印消息
--- @param ... any
function game:say(...)
    local args = table.pack(...)
    local message = ""
    for i = 1,#args do
        message = message .. tostring(args[i]) .. ' '
    end
    jass.DisplayTextToPlayer(jass.GetLocalPlayer(),0,0, message)
end 

--- 清空屏幕
function game:clear()
    return jass.ClearTextMessages()
end

--- 游戏(暂停/恢复)
--- @param flag boolean
function game:pause(flag)
    jass.PauseGame(flag)
end

--- 黑色阴影(启用/禁用)
--- @param flag boolean
function game:fogmask(flag)
    jass.FogMaskEnable(flag)
end

--- 战争迷雾(启用/禁用)
--- @param flag boolean
function game:fog(flag)
    jass.FogEnable(flag)
end

--- 输入作弊码
--- @param message string
function game:cheat(message)
    jass.Cheat(message)
end

--- 窗口是否激活
--- @return boolean
function game:is_active()
    return japi.IsWindowActive()
end

--- 判断输入状态
--- @return boolean
function game:is_chating()
    return japi.GetChatState()
end

--- 获取游戏时间
--- @return number
function game:get_time()
    return jass.GetFloatGameState(jass.GAME_STATE_TIME_OF_DAY)
end

-- 设置可用镜头区域
function game:set_camera_bounds(rect)
	local minX, minY, maxX, maxY = rect:get()
	jass.SetCameraBounds(minX, minY, minX, maxY, maxX, maxY, maxX, minY)
end 

-- 设置远景
function game:set_camera_farz(value )
    jass.SetCameraField(jass.CAMERA_FIELD_FARZ, value, 0)
end 

--- 设置当前游戏时间
function game:set_time(whatTime)
    jass.SetFloatGameState(jass.GAME_STATE_TIME_OF_DAY, whatTime)
    if ac.game:in_daytime() then 
        ac.game:notify("游戏-时间变化", "白天")
    elseif ac.game:in_nighttime() then
        ac.game:notify("游戏-时间变化", "夜晚")
    end 
end

--- 设置昼夜流逝速度 0 - 100
function game:set_day_scale(scalePercent)
    jass.SetTimeOfDayScale(scalePercent * 0.01)
end

--- 玩家是否按下某个键
function game:iskeydown(key)
    return japi.GetKeyState(KEY[key])
end

--- 判断游戏处于录像
function game:is_video()
    return (japi.SimpleFrameFindByName("SimpleReplayPanel", 0) ~= 0)
end

--- 处于白天
function game:in_daytime()
    local time = game:get_time()
    return time >= 6.0 and time < 18.0
end

--- 处于夜晚
function game:in_nighttime()
    local time = game:get_time()
    return time >= 18.0 or time < 6.0
end

--- 小地图ping点
function game:ping(dest,time)
	local x,y = dest:get_point():get()
    jass.PingMinimap(x,y, time or 1)
end


ac.event.register("游戏-时间变化", function()
    if ac.game:in_daytime() then 
        ac.game:notify("游戏-时间变化", "白天")
    elseif ac.game:in_nighttime() then
        ac.game:notify("游戏-时间变化", "夜晚")
    end 
end)     

return game